package com.npsngs.hawkeye.components;

import com.npsngs.hawkeye.MainActivity;
import com.npsngs.hawkeye.R;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class GrayingDialog extends DialogShell implements OnSeekBarChangeListener{
	private SeekBar seekBar_r = null;
	private SeekBar seekBar_g = null;
	private SeekBar seekBar_b = null;
	private TextView rate_r = null;
	private TextView rate_g = null;
	private TextView rate_b = null;
	private View view = null;
	private int r = 30;
	private int g = 60;
	private int b = 10;
	public GrayingDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		view = View.inflate(context, R.layout.dialog_graying, null);
		seekBar_r = (SeekBar) view.findViewById(R.id.gray_red_seekBar);
		seekBar_g = (SeekBar) view.findViewById(R.id.gray_green_seekBar);
		seekBar_b = (SeekBar) view.findViewById(R.id.gray_blue_seekBar);
		
		rate_r = (TextView) view.findViewById(R.id.gray_red_textView);
		rate_g = (TextView) view.findViewById(R.id.gray_green_textView);
		rate_b = (TextView) view.findViewById(R.id.gray_blue_textView);
		
		seekBar_r.setOnSeekBarChangeListener(this);
		seekBar_g.setOnSeekBarChangeListener(this);
		seekBar_b.setOnSeekBarChangeListener(this);
		
	}

	@Override
	public Dialog getDialog() {
		// TODO Auto-generated method stub
		dialog = builder
				.setView(view)
				.setNegativeButton(R.string.cancel, null)
				.setPositiveButton(R.string.sure,null).create();
		return dialog;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		if(!fromUser){
			switch(seekBar.getId()){
			case R.id.gray_red_seekBar:
				r = progress;
				rate_r.setText(r+"%");
				break;
			case R.id.gray_green_seekBar:
				g = progress;
				rate_g.setText(g+"%");
				break;
			case R.id.gray_blue_seekBar:
				b = progress;
				rate_b.setText(b+"%");
				break;
			}
			return;
		}
		int delta = 0;
		switch(seekBar.getId()){
		case R.id.gray_red_seekBar:
			delta = progress - r;
			r = progress;
			g -= delta/2;
			b -= (delta-delta/2);
			break;
		case R.id.gray_green_seekBar:
			delta = progress - g;
			g = progress;
			r -= delta/2;
			b -= (delta-delta/2);
			break;
		case R.id.gray_blue_seekBar:
			delta = progress - b;
			b = progress;
			r -= delta/2;
			g -= (delta-delta/2);
			break;
		}
		rate_r.setText(r+"%");
		rate_g.setText(g+"%");
		rate_b.setText(b+"%");
		seekBar_r.setProgress(r);
		seekBar_g.setProgress(g);
		seekBar_b.setProgress(b);
		MainActivity.nativeGraying(r/100f,g/100f,b/100f);
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

}
